# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Team, User, Producer, Video
from backend.libs.args_util import PageArgs


class TeamListController(BaseController):

    def get(self):
        args = PageArgs(self, 'get', 'name')
        t = Team.objects
        
        t = t.order_by('-create_time')
        paginator = self.pagination(t.all(), number_per_page=args.pageSize, page_num=args.pageNum)
        return self.render_json(data=paginator)


class ListAll(BaseController):

    def get(self):
        teams = Team.objects.order_by('score_total').all()
        data = [ {'label': t.name, 'value': t.id} for t in teams ]
        return self.render_json(data={'teams': data})


class TeamDetailController(BaseController):

    def get(self):
        id = self.int_argument('id', -1)
        if id <= 0:
            return self.render_json(code=1001, msg='参数错误')
        team = Team.objects.filter(id=id).first()
        return self.render_json(data=team.to_dict())


class BaseTeamController(BaseController):

    def save_team(self, team, data):
        team.name = data.get('name')
        team.city = data.get('city', '')
        team.desc = data.get('desc', '')
        team.honer = data.get('honer', '')
        tmp_score = data.get('score', 0)
        if tmp_score:
            team.score = tmp_score
        tmp_score = data.get('score_total', 0)
        if tmp_score:
            team.score_total = tmp_score
        tmp_score_num = data.get('score_num', 0)
        if tmp_score_num:
            team.score_num = tmp_score_num
        owner_id = data.get('owner_id')
        owner = None
        if owner_id and int(owner_id) > 0:
            owner = User.objects.filter(id=owner_id).first()
        team.owner = owner
        team.save()


class CreateTeamController(BaseTeamController):

    def post(self):
        data = json.loads(self.request.body)
        team_name = data.get('name', '')
        if not team_name:
            return self.render_json(code=1001, msg='参数错误')
        team = Team()
        self.save_team(team, data)
        return self.render_json(data=team.to_dict())


class UpdateTeamController(BaseTeamController):

    def post(self):
        data = json.loads(self.request.body)
        id = data.get('id')
        team_name = data.get('name', '')
        if not team_name or not id or int(id) <= 0:
            return self.render_json(code=1001, msg='参数错误')
        team = Team.objects.filter(id=id).first()
        if not team:
            return self.render_json(code=1001, msg='团队不存在')
        self.save_team(team, data)
        return self.render_json(data=team.to_dict())


class DeleteTeamController(BaseController):

    def post(self, team_id):
        team_id = int(team_id)
        if team_id > 0:
            team = Team.objects.filter(id=team_id).first()
            if team:
                videos = Video.objects.filter(team_id=team_id).all()
                if videos:
                    return self.render_json(code=1001, msg='团队存在视频关联，无法删除')
                user = User.objects.filter(id=team.owner_id).first()
                if user:
                    user.user_state = 3
                    user.save()

                producers = Producer.objects.filter(team_id=team_id).all()
                for p in producers:
                    p.team_id = None
                    p.save()
                team.delete()

        return self.render_json()
